﻿var sys_mgr = {
    queuelist: [],
	$queue_div:null,
    queue_panel: null,
    add_user_dlg:null,
	management_mode:false,
	selectQInst:null,
	q_refresh:function(){
		$.getJSON(
			"/www/json_service.yaws?mod=queue&action=list",
			function(data){
				$("div#out_queues_area",sys_mgr.$queue_ui).empty();
				$("div#in_queues_area",sys_mgr.$queue_ui).empty();
				if(data.result == "error")
				{
					sys_mgr.$queue_ui.text(data.data);
					return;
				}
				sys_mgr.queue_list = new Array();
				$.each(data.data,function(index,queue_info){
					q = new QueueInst(queue_info,sys_mgr.$queue_ui);
					q.refreshCusts();
					sys_mgr.queue_list[queue_info.id] = q;
				});
			});
	}
};
function init_sys(){
    //sys_mgr.queue_panel = new QueuePanel($("div#queue_panel"));
    //sys_mgr.act_dlg = new QActionDlg($("div#cmn_dyn_frmdlg"));
}
function load_queue($ui){
	sys_mgr.$queue_ui = $ui;
	sys_mgr.q_refresh();
}

/*******

    Class QueueInst, map to server-side queue-inst
*/
function QueueInst(data,container){
	var scrollPane,scrollContent;
    var q_inst = this;
	this.data = data;
    this.custs = Array();
    this.container = container;
	
	this.in_ui = $("<div></div>").addClass("queue_node").appendTo($("#in_queues_area",this.container));
	this.out_ui = $("<div></div>").addClass("queue_node").appendTo($("#out_queues_area",this.container));
	this.in_ui.data("inst",this);//绑定数据
	this.out_ui.data("inst",this);
	
	$("<div></div>")
		.addClass("ui_icon")
		.addClass("shadow")
		.addClass("s1") //changed to the fixed icon. p=this.data.icon_type
		.append($("<div></div>").addClass("icon_top_left"))
		.append($("<div></div>").addClass("icon_bottom_right"))
		.appendTo(this.in_ui)
		.click(function(ev){
			//q_inst.container.addClass("lite");
			//q_inst.container.hide();
			q_inst.container.fadeOut();
			sys_mgr.add_user_dlg.show(q_inst);
			ev.preventDefault();
		});
	$("<div></div>")
		.addClass("ui_icon")
		.addClass("shadow")
		.addClass("s4") // changed to the fixed icon. p=this.data.icon_type
		.append($("<div></div>").addClass("icon_center"))
		.appendTo(this.out_ui)
		.append($("<div></div>").addClass("icon_bottom"))
		.click(function(ev){
			if(q_inst.custs.length > 0){
				q_inst.requestRemoveCust(q_inst.custs[0].index);
			}
			ev.preventDefault();
		});
}

QueueInst.prototype.refresh = function(){
	$(".icon_top_left",this.in_ui).text(this.data.prefix);
	$(".icon_bottom_right",this.in_ui).text(this.custs.length.toString());
	$(".icon_center",this.out_ui).html(this.data.prefix);
	if(this.custs.length > 0){
		if(this.custs[0].mobile.length >= 9){
			short_num = this.custs[0].mobile.substring(this.custs[0].mobile.length - 9,8);
		}else
		{
			short_num = "XXXX";
		}
		$(".icon_bottom",this.out_ui).html(this.data.prefix + this.custs[0].index.toString() + "  " + short_num);
	}else
		$(".icon_bottom",this.out_ui).html("空");
}
QueueInst.prototype.refreshCusts = function(){
    var url =  "/www/json_service.yaws?mod=queue&action=detail&id=" + this.data.id;
    var q_inst = this;
    $.getJSON(url,function(data){
		$.each(data.data,function(index,C){
			q_inst.addCust(C);
		});
		q_inst.refresh();
    });
}
QueueInst.prototype.addCust = function(Cust){
	var cust_ui;
	this.custs.push(Cust);
	this.refresh();
}
QueueInst.prototype.removeCust = function(C_Index){
	var temp = this.custs;
	var q_inst = this;
	this.custs = new Array();
	$.each(temp,function(index,C){
			if(C.index != C_Index)
				q_inst.custs.push(C);
		});
	this.refresh();
}


QueueInst.prototype.renderCust = function(C){
	var short_num;
	if(C.mobile.length >= 9){
		short_num = C.mobile.substring(C.mobile.length - 9,8);
	}else
	{
		short_num = "XXXX";
	}
	return $("<dd></dd>").append(vsprintf("%6s %8s %6s %4d",[(this.data.prefix + C.index), short_num, C.name,C.sms_sent_times])).data("id",C.index);
}

QueueInst.prototype.renderCustBrief = function(C){
	return $("<dd></dd>").append(this.data.prefix + C.index + "  " + C.mobile + "   " + C.name + "  " + C.sms_sent_times).data("id",C.index);
}
QueueInst.prototype.renderCustTableItem = function(C){
	var short_num;
	if(C.mobile.length >= 9){
		short_num = C.mobile.substring(C.mobile.length - 9,8);
	}else
	{
		short_num = "XXXX";
	}
	return $("<tr></tr>").addClass("item").append(vsprintf("<td>%s</td> <td>%s</td> <td>%s</td> <td>%4d</td>",[(this.data.prefix + C.index), short_num, C.name,C.sms_sent_times])).data("id",C.index);
}

/*
obsolete it
*/
QueueInst.prototype.refreshCustUI = function($cust_area)
{
	var C;
	var q_inst = this;
	$(".cust_list",$cust_area).empty();
	$.each(this.custs,function(index,C){
		//$("div.cust_content",q_inst.ui).append(q_inst.renderCust(C));
		q_inst.renderCust(C).appendTo($(".cust_list",$cust_area));
	});
}
QueueInst.prototype.findCustByIndex = function(index){
	var cust = undefined;
	for(i = 0;i < this.custs.length;i++){
		if(this.custs[i].index == index){
			cust = this.custs[i];
			break;
		}
	}
	return cust;
}
QueueInst.prototype.requestRemoveCust = function(cust_index){
	var q_inst = this;
	var cust = this.findCustByIndex(cust_index);
	if(cust != undefined){
		$( "#model_dlg p" ).text(vsprintf("将顾客%6s(%6s)移出队列",[(this.data.prefix + cust_index), cust.name]));
        $("body").prepend("<div class='popup-bg'></div>"); //newly added
 	    $(".popup-bg").fadeTo('slow',0.3);
        var outWinWidth = 450;
        var outWinHeight = 342;
        var outLeftMargin = ($(window).width() - outWinWidth)/2;
        var outTopMargin = ($(window).height() - outWinHeight)/1.6;
		$("#model_dlg" ).animate({left:outLeftMargin,bottom:outTopMargin,opacity:'show',width:outWinWidth,height:outWinHeight,},500);
  		$(".outWinClose").click(function(){ //close window
  			$("#model_dlg").fadeOut();
  			$(".popup-bg").remove();
  			
  		});
  		$(".ser_ok").click(function(){ //close window
  			q_inst.requestRemoveCustAction(cust_index,"served");  //原ebiz_xy.js中175行语句，参考
  			$("#model_dlg").fadeOut();
  			$(".popup-bg").remove();
  			
  		});
  		$(".ser_missed").click(function(){
  			q_inst.requestRemoveCustAction(cust_index,"skipped")  //原ebiz_xy.js中语句，参考
  			$(this).fadeOut();
  			$(".popup-bg").remove();  			
  		});
	
		/* 0928 removed
		$( "#model_dlg" ).dialog({
			resizable: false,
			height:140,
			modal: true,
			buttons: {
				"服务顾客": function() {
					q_inst.requestRemoveCustAction(cust_index,"served");
					$( this ).dialog( "close" );
				},
				"跳过": function() {
					q_inst.requestRemoveCustAction(cust_index,"skipped")
					$( this ).dialog( "close" );
				},
				"取消":function(){
					$(this).dialog("close");
				}
			}
		});*/
	}
}
QueueInst.prototype.requestRemoveCustAction = function(cust_index,tag){	
			var q_inst = this;
			$.ajax({
				url:"/www/queue_act.yaws?mod=queue&action=del_cust&id=" + this.data.id + "&cust_id=" + cust_index + "&tag=" + tag,
				context:this
			}).done(function(data){
				q_inst.removeCust(cust_index);
				sys_mgr.add_user_dlg.refresh();
				});
}
		
function QOpArea($div){
	var myself = this;
	this.ui = $div;
	/* 0928 removed
	$("a#close_bt",this.ui).click(function(ev){
		$div.fadeOut();
		myself.q_inst.container.fadeIn();
		//myself.q_inst.container.removeClass("lite");
		ev.preventDefault();
	});
	*/
	$("button#ok",this.ui).click(function(ev){
		var data = {
			"name":$("#name",myself.ui).val(),
			"mobile":$("#mobile",myself.ui).val(),
		};
		ev.preventDefault();
		if($("#queue_op_form").valid() == false)return;
		$.ajax({
			type:"POST",
			dataType:"json",
			content:"json",
			"data":JSON.stringify(data),
					url:"/www/json_service.yaws?mod=queue&action=add_cust&id=" + myself.q_inst.data.id,									
			success:function(data){
				if(data.result == "error"){
					$(".info_area",myself.ui).text(data.data);
				}else{
					myself.q_inst.addCust(data);
					myself.refresh();
					$(".info_area",myself.ui).text(
					"排队号:" + data.index.toString() + "前面还有" + data.loc.toString() + "位");
					//myself.ui.hide();
					//myself.q_inst.container.show();
				}
			}
		});
		ev.preventDefault();
	});
	$("table#in_cust_table").click(function(ev){
		var cust_index = $(ev.target).parent("tr.item").data("id");
		if(cust_index == undefined) return;
		myself.q_inst.requestRemoveCust(cust_index);
	});
	$(".adv_tag",this.ui).click(function(ev){
		$(".adv_panel",this.ui).toggle();
	});
	$("#adv_ok",this.ui).click(function(ev){
				$.ajax({
					url:"/www/queue_act.yaws?mod=queue&action=adv_set&id=" + myself.q_inst.data.id + "&adv_val=" + $("#notify_cond",myself.ui).val(),
					context:this
					})
				.done(function(data){
				});
				
			}
	);
			//1001: 似乎没走下去，得另外定义方法？
		var tableSwitch = 1;// new Boolean(1);
		$(".detail_tag",this.ui).click(function(ev){
		    if(tableSwitch==1){
			   myself.ui.animate({height: 640},200);
		       $(".main_area").css({display:'block'});
		       $(".detail_tag",this.ui).html("收起");
		       tableSwitch = 0;
		       return(tableSwitch);
		     }else{
	         $(".main_area").css({display:'none'});
	         $(".detail_tag",this.ui).html("明细");
		       myself.ui.animate({height: 342},200);
		       tableSwitch = 1;
		       return(tableSwitch);
		     }
		});
		$(".inWinClose",this.ui).click(function(ev){
    	  	  $(".main_area").css({display:'none'});
	          $(".detail_tag",this.ui).html("明细");
	          myself.ui.animate({height: 340},200);
		      myself.ui.fadeOut(500);
		      $(".popup-bg",this.ui).remove();
		      tableSwitch = 1;
			  myself.q_inst.container.fadeIn()
		      return(tableSwitch);
	    });

	this.q_inst = null;
}
QOpArea.prototype.show = function(q_inst){
	//if (this.q_inst != q_inst)
	this.q_inst = q_inst;
		    var inWinWidth = 450;
            var inWinHeight = 342;
			var inLeftMargin = ($(window).width() - inWinWidth)/2;
            var inTopMargin = ($(window).height() - inWinHeight)/1.6;
		
			$("body").prepend("<div class='popup-bg'></div>"); //0928新加，用于打开添加用户界面
			$(".popup-bg").fadeTo('slow',0.3);
	this.refresh();
	this.ui.animate({top:50,left:inLeftMargin,opacity:'show',width:450,height:342,},500); //fadeIn();
	

}
QOpArea.prototype.refresh = function(){
	var q_inst = this.q_inst;
	$("#name",this.ui).val("");
	$("#mobile",this.ui).val("");
	if(this.q_inst.data["notify_cond"] == undefined)
		$("#notify_cond",this.ui).val("0");
	else
		$("#notify_cond",this.ui).val(this.q_inst.data.notify_cond.toString());
	
	$("div.info_area",this.ui).text("当前排队数:" + this.q_inst.custs.length.toString());
	$(".title_txt",this.ui).text(this.q_inst.data.name);
	
	$("div.cust_area table#in_cust_table tr.item",this.ui).remove();
	$.each(q_inst.custs,function(index,C){
		q_inst.renderCustTableItem(C).appendTo("div.cust_area table#in_cust_table");
	});
	$("div.cust_area table#in_cust_table tr.item:even",this.ui).addClass("even");
}

function check_mobile(){
	this.status = "false";
	this.mobile = "";
	this.verify_data = "";
}
check_mobile.prototype.verifyMobile = function(m,verify_data,f){
	if(this.mobile == m){
		return this.status;
	}else{
		var thisInstance = this;
		this.mobile = m;
		this.verify_data = verify_data;
		this.status = "inprogress";
		$.get("/www/unauth_act.yaws?action=check_user&mod=user&name=" + m,function(data){
			thisInstance.setResult(data);
			if(f != undefined)
				f(thisInstance.getResult());
		});
	}
}
check_mobile.prototype.setResult = function(data){
	if(data.replace(/(\s*$)/g,"") == this.verify_data){
		this.status = "expected";
	}else{
		this.status = "no_expected";
	}
}

check_mobile.prototype.getResult = function(){
	return this.status;
}

